package jeme.lang;

import java.util.HashMap;

/**
 *
 *
 * @author Erik Silkensen (silkense@colorado.edu)
 * @version Jan 24, 2009
 */
public class SchemeSymbol extends SchemeObject
{
    private static final HashMap<String, SchemeSymbol> symbols;
    
    private static final HashMap<String, Integer> instances;
    
    static 
    { 
        symbols = new HashMap<String, SchemeSymbol>(); 
        instances = new HashMap<String, Integer>();
    }
    
    /**
     * 
     * @param symbol
     * @return
     */
    public static SchemeSymbol getInstance(String symbol)
    {
       if (!symbols.containsKey(symbol)) {
           symbols.put(symbol, new SchemeSymbol(symbol)); 
           instances.put(symbol, 0);
       }
       instances.put(symbol, instances.get(symbol) + 1);
       return symbols.get(symbol);
    }
    
    /**
     * 
     * @param symbol
     */
    public static void remove(String symbol)
    {
        Integer freq = instances.get(symbol);
        if (freq != null) {
            freq--;
            if (freq == 0) {
                instances.remove(symbol);
                symbols.remove(symbol);
            } else {
                instances.put(symbol, freq);
            }
        }
    }
    
    private String symbol;
    
    /**
     * 
     * @param symbol
     */
    private SchemeSymbol(String symbol)
    {
        setSymbol(symbol);
    }
    
    /**
     * 
     * @return
     */
    protected String getSymbol()
    {
        return symbol;
    }
    
    /**
     * 
     * @param symbol
     */
    protected void setSymbol(String symbol)
    {
        this.symbol = symbol;
    }
    
    /*
     * (non-Javadoc)
     * @see jeme.lang.SchemeObject#toString()
     */
    public String toString()
    {
        return symbol;
    }
}
